<!DOCTYPE html>
<html lang="zh" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('商品信息列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <p>所属品牌：</p>
                            <select name="brandId">
                                <option value="">请选择商品品牌</option>
                                <option th:each="brand : ${brandList}" th:value="${brand.id}"
                                        th:text="${brand.name}"></option>
                            </select>
                        </li>
                        <li>
                            <p>所属分类：</p>
                            <input id="treeId" name="productCategoryId" type="hidden"
                                   th:value="${productCategory?.id}"/>
                            <input class="form-control" type="text" onclick="selectCategoryTree()" id="treeName"
                                   readonly="true" th:value="${productCategory?.name}">
                        </li>
                        <li>
                            <p>名称：</p>
                            <input name="name" type="text"/>
                        </li>
                        <li>
                            <input type="hidden" id="storeId" th:value="${storeId}">
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">

            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="mall:product:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table data-mobile-responsive="true" id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('mall:product:edit')}]];
    var removeFlag = [[${@permission.hasPermi('mall:product:remove')}]];
    var prefix = ctx + "mall/cooper";

    var storeId = $("#storeId").val();
    $(function () {
        var options = {
            url: prefix + "/productList?storeId=" + storeId,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "商品信息",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '编号',
                    visible: false
                },

                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'pic',
                    title: '图片'
                    ,
                    formatter: function (value, row, index) {
                        return '<img src="' + row.pic + '" width="50px" height="50px"> ';
                    }
                },
                {
                    field: 'productSn',
                    title: '货号'
                },
                {
                    field: 'brandName',
                    title: '品牌名称'
                },
                {
                    field: 'productCategoryName',
                    title: '商品分类名称'
                },

                {
                    field: 'storeId',
                    title: '所属店铺'
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '上架状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return publishStatusTools(row);
                    }
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '推荐状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return recommandStatusTools(row);
                    }
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '审核状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return verifyStatusTools(row);
                    }
                },
                {
                    field: 'sale',
                    title: '销量'
                },
                {
                    field: 'price',
                    title: '价格'
                },
                {
                    field: 'originalPrice',
                    title: '市场价'
                },
                {
                    field: 'stock',
                    title: '库存'
                },
                {
                    field: 'lowStock',
                    title: '库存预警值',
                },
                {
                    title: '操作',
                    width: '17%',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="addSubject(\'' + row.id + '\')"><i class="fa fa-add"></i>添加到专题</a>');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function publishStatusTools(row) {
        if (row.publishStatus == 0) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enablePublishStatus(\'' + row.id + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disablePublishStatus(\'' + row.id + '\')"></i> ';
        }
    }

    function disablePublishStatus(id) {
        $.modal.confirm("确认要下架此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "publishStatus": 0});
        })
    }

    function enablePublishStatus(id) {
        $.modal.confirm("确认要上架此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "publishStatus": 1});
        })
    }

    function recommandStatusTools(row) {
        if (row.recommandStatus == 0) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enableRecommandStatus(\'' + row.id + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disableRecommandStatus(\'' + row.id + '\')"></i> ';
        }
    }

    function disableRecommandStatus(id) {
        $.modal.confirm("确认要不推荐此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "recommandStatus": 0});
        })
    }

    function enableRecommandStatus(id) {
        $.modal.confirm("确认要推荐此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "recommandStatus": 1});
        })
    }

    function verifyStatusTools(row) {
        if (row.verifyStatus == 0) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enableVerifyStatus(\'' + row.id + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disableVerifyStatus(\'' + row.id + '\')"></i> ';
        }
    }

    function disableVerifyStatus(id) {
        $.modal.confirm("确认要拒绝此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "verifyStatus": 0});
        })
    }

    function enableVerifyStatus(id) {
        $.modal.confirm("确认要通过此商品吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"id": id, "verifyStatus": 1});
        })
    }

    /*产品分类-新增-选择父部门树*/
    function selectCategoryTree() {
        var options = {
            title: '产品分类选择',
            width: "380",
            url: ctx + "mall/category" + "/selectCategoryTree/" + $("#treeId").val(),
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    function addSubject(id) {
        //window.location.href("mall/attribute?productAttributeCategoryId="+productAttributeCategoryId+"&type="+0);
        $.modal.open("添加商品专题", "/mall/subject/addSubject?productId=" + id);
    }
</script>
<script type="text/javascript">
</script>
</body>
</html>
